//
//  ReadListViewController.m
//  Leisure
//
//  Created by xalo on 16/6/29.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "ReadListViewController.h"

@interface ReadListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UITableView *newestTableView;//最新的列表
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;//最热的列表
@property (nonatomic,strong)UISegmentedControl *segmentedControl;//分段控制
@property (nonatomic,strong)NSMutableArray *newestDataSource;//最新的数据源
@property (nonatomic,strong)NSMutableArray *hotDataSource;//最热的数据源

@property (nonatomic,assign)NSInteger newStart;//最新列表的数据起始位置
@property (nonatomic,assign)NSInteger newLimit;//最新列表的数据的条数
@property (nonatomic,assign)NSInteger hotStart;//最热列表的饿数据起始位置
@property (nonatomic,assign)NSInteger hotLimit;//最热列表的数据的条数

@end

@implementation ReadListViewController

- (UISegmentedControl *)segmentedControl{
    if (!_segmentedControl) {
        _segmentedControl=[[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        [_segmentedControl insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
        [_segmentedControl insertSegmentWithTitle:@"最热" atIndex:1 animated:YES];
        //添加点击事件
        [_segmentedControl addTarget:self action:@selector(clickSegmentedControl:) forControlEvents:UIControlEventValueChanged];
    }
    return _segmentedControl;
}
- (NSMutableArray *)newestDataSource{
    if (!_newestDataSource) {
        _newestDataSource=[NSMutableArray array];
    }
    return _newestDataSource;
}

- (NSMutableArray *)hotDataSource{
    if (!_hotDataSource) {
        _hotDataSource=[NSMutableArray array];
    }
    return _hotDataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.navigationItem.titleView=self.segmentedControl;
    //注册
    [self.newestTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.hotTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //请求数据
    [self loadNewestTableViewDataSourceWithStart:0 limit:10];
    [self loadHotTableViewDataSourceWithStart:0 limit:10];
    //下拉刷新
    self.newestTableView.mj_header=[MJRefreshStateHeader headerWithRefreshingBlock:^{
       //刷新之前数组清空
        [self.newestDataSource removeAllObjects];
        self.newStart = 0;
        [self loadNewestTableViewDataSourceWithStart:0 limit:20];
    }];
    self.hotTableView.mj_header=[MJRefreshStateHeader headerWithRefreshingBlock:^{
       //刷新之前数组清空
        [self.hotDataSource removeAllObjects];
        self.hotStart += 0;
        [self loadHotTableViewDataSourceWithStart:0 limit:20];
    }];
    //上拉加载
    self.newestTableView.mj_footer=[MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.newStart +=20;
        [self loadNewestTableViewDataSourceWithStart:0 limit:20];
    }];
    self.hotTableView.mj_footer=[MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        self.hotStart +=20;
        [self loadHotTableViewDataSourceWithStart:0 limit:20];
    }];
    
    
}

#pragma mark---------数据请求--------------------
//加载最新列表的数据
- (void)loadNewestTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    
    [RequestManager fetchDataWithUrl:kReadListUrl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"addtime"} method:@"POST" complete:^(NSData *data) {
        NSDictionary *bigDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //NSLog(@"%@",bigDic);
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            ReadListModel *model=[[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.newestDataSource addObject:model];
        }
    } updateUI:^{
        //结束上拉加载
        [self.newestTableView.mj_footer endRefreshing];
       
        //结束下拉刷新
        [self.newestTableView.mj_header endRefreshing];
       
        //NSLog(@"刷新UI");
        [self.newestTableView reloadData];
    }];
}

//加载最热列表的数据
- (void)loadHotTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithUrl:kReadListUrl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"hot"} method:@"POST" complete:^(NSData *data) {
        NSDictionary *bigDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //NSLog(@"%@",bigDic);
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            ReadListModel *model=[[ReadListModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.hotDataSource addObject:model];
        }

    } updateUI:^{
        //结束上拉加载
         [self.hotTableView.mj_footer endRefreshing];
        //结束下拉刷新
         [self.hotTableView.mj_header endRefreshing];
        [self.hotTableView reloadData];
    }];
}
//点击事件
- (void)clickSegmentedControl:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex==0) {
        
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
    }else{
        [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}

#pragma  mark-------------UIScrollView的协议--------------------

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView==self.scrollView) {
        
        if (self.scrollView.contentOffset.x==0) {
            self.segmentedControl.selectedSegmentIndex=0;
        }else if (self.scrollView.contentOffset.x==kWidth){
            self.segmentedControl.selectedSegmentIndex=1;
        }
    }
}






#pragma mark----------UITableView的协议----------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView==self.newestTableView) {
      
        return  self.newestDataSource.count;
    }else{
       
        return self.hotDataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    //创建一个model来接收
    ReadListModel *model=[[ReadListModel alloc]init];
    if (tableView==self.newestTableView) {
        model=self.newestDataSource[indexPath.row];
    }else{
        model=self.hotDataSource[indexPath.row];
    }
    cell.textLabel.text=model.title;
    
    return cell;
}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
